$(function () {
    window.g_request_prefix = '';
    window.g_status_nologin = 401;
    window.g_status_error = 500;
});

function ajaxPost(url, data, callBack) {// POST请求
    ajaxCommon('POST', true, url, data, callBack, true, false);
}

function ajaxPostNoL(url, data, callBack) {// POST请求[没有loading]
    ajaxCommon('POST', true, url, data, callBack, false, false);
}

function ajaxGet(url, data, callBack) {// GET请求
    ajaxCommon('GET', true, url, data, callBack, true, false);
}

function ajaxGetNoL(url, data, callBack) {// GET请求
    ajaxCommon('GET', true, url, data, callBack, false, false);
}

function ajaxFile(url, data, callBack) {// POST请求
    ajaxCommon('POST', true, url, data, callBack, true, 'upload');
}

/**
 * create by fuhuichao 通用的ajax请求入口
 * @param type 'GET'||'POST'， 默认为GET
 * @param isAsync 是否异步 true：异步，false：同步
 * @param url 请求地址
 * @param data 参数json
 * @param callBack 回调函数
 * @param isLoading 是否需要显示遮罩
 * @param contentType 内容编码类型：'upload': false, true: application/json, false: application/x-www-form-urlencoded
 */
function ajaxCommon(type, isAsync, url, data, callBack, isLoading, contentType) {// AJAX构造方法
    // 当需要调用其他项目的接口时，定制开发
    url = g_request_prefix + url;

    // 给每个请求添加随机数，防止缓存
    url += ((url.indexOf('?') > -1) ? '&' : '?') + 'rn=' + dateUtil.getTime();
    // 根据提交参数类型不同，设置不同的ajax提交方式
    var processDataVal, contentTypeVal;
    if (contentType == 'upload') {
        processDataVal = false;
        contentTypeVal = false;
    } else {
        processDataVal = true;
        contentTypeVal = contentType ? 'application/json' : 'application/x-www-form-urlencoded; charset=UTF-8'
    }

    // 开发环境，跳过登录验证
    //  url = url.replace('.do', '.go');

    if (isLoading) showLoading();
    $.ajax({
        type: type,
        url: url,
        async: isAsync,
        processData: processDataVal,// 默认值为true
        contentType: contentTypeVal,
        data: data || {},
        success: function (response) {
            try {
                if (response == '') return;
                response = JSON.parse(response);
                if (response.status == g_status_nologin) {
                    top.location.href = response.data.loginUrl;
                }
                else if (response.status == g_status_error) {
                    alertServerError(response.message);
                }
                else {
                    if (callBack) callBack(response.data);
                }
            } catch (e) {
                alertJSError(e);
            } finally {
                if (isLoading) hideLoading();
            }
        },
        error: function () {
            if (isLoading) hideLoading();
            // alertServerError();
        }
    });
}


function alertJSError(msg) {
    alert(msg);
}

function alertServerError(msg) {
    alert(msg);
}

// 显示遮罩
function showLoading() {
    try {
        $('body').FHCLoading("show");
    } catch (e) {
        try {
            top.$('body').FHCLoading("show");
        } catch (e) {

        }
    }
}

// 隐藏遮罩
function hideLoading() {
    try {
        $('body').FHCLoading("hide");
    } catch (e) {
        try {
            top.$('body').FHCLoading("hide");
        } catch (e) {

        }
    }
}
